package com.example.xlmkotlinwanandroid.ui.adapter

import android.content.Context
import android.content.Intent
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.example.xlmkotlinwanandroid.R
import com.example.xlmkotlinwanandroid.model.bean.ArticleBean
import com.example.xlmkotlinwanandroid.ui.activity.WebActivity

/**
 * 首页所需要的adapter
 * 第一遍先用最基本的方式来写，熟练了再做更换吧
 *
 */
class ArticleAdapter(var context: Context?) :
    RecyclerView.Adapter<ArticleAdapter.ViewHolder>() {

    private val inflater: LayoutInflater = LayoutInflater.from(context)
    private var dataList = ArrayList<ArticleBean>()

    fun setData(datas: List<ArticleBean>) {
        dataList = datas as ArrayList<ArticleBean>
    }


    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val itemView = inflater.inflate(R.layout.item_ariticle, parent,false)
        return ViewHolder(itemView)
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        var bean = dataList[position]
        holder.homeAuthor.text = bean.author
        holder.homeTop.text = bean.superChapterName
        holder.homeDate.text = bean.niceDate
        holder.homeContent.text = bean.title
        holder.homeType2.text = "${bean.superChapterName}:${bean?.author}"

        //设置点击事件
        holder.itemView.setOnClickListener {
            val intent = Intent(context,WebActivity::class.java)
            intent.putExtra("url",bean.link)
            intent.putExtra("title",bean.title)
            context?.startActivity(intent)
        }

    }

    override fun getItemCount(): Int {
        return if (dataList.size > 0) {
            dataList.size
        } else {
            0
        }
    }

    class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val homeAuthor: TextView = itemView.findViewById(R.id.item_home_author)
        val homeTop: TextView = itemView.findViewById(R.id.item_home_top)
        val homeNew: TextView = itemView.findViewById(R.id.item_home_new)
        val homeType1: TextView = itemView.findViewById(R.id.item_home_type1)
        val homeDate: TextView = itemView.findViewById(R.id.item_home_date)
        val homeContent: TextView = itemView.findViewById(R.id.item_home_content)
        val homeType2: TextView = itemView.findViewById(R.id.item_home_type2)
    }
}